<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['medicine:outrecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['medicine:outrecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['medicine:outrecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['medicine:outrecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库类型" prop="storageOutType" style="width: 300px">
        <el-select v-model="queryParams.storageOutType" placeholder="请选择出库类型" clearable>
          <el-option
            v-for="dict in medicine_our_record"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="reviewStatus" style="width:300px">
        <el-select v-model="queryParams.reviewStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in medicine_shenhe"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出库单号" prop="storageOutId" style="width: 350px">
        <el-input
            v-model="queryParams.storageOutId"
            placeholder="请输入出库单号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="outrecordList" @selection-change="handleSelectionChange">
      <el-table-column label="出库单号" align="center" prop="storageOutId" />
      <el-table-column label="出库类型" align="center" prop="storageOutType">
        <template #default="scope">
          <dict-tag :options="medicine_our_record" :value="scope.row.storageOutType"/>
        </template>
      </el-table-column>
      <el-table-column label="出库人员 " align="center" prop="medicineAdminOut" />
      <el-table-column label="制单人员" align="center" prop="medicineAdminOrder" />
      <el-table-column label="创建日期" align="center" prop="orderDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购金额" align="center" prop="buyMoney" />
      <el-table-column label="发售金额" align="center" prop="sellMoney" />
      <el-table-column label="审核状态" align="center" prop="reviewStatus" />


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300px">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['medicine:record:edit']" v-if="scope.row.reviewStatus!=='未审核'">查看</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['medicine:record:edit']" v-if="scope.row.reviewStatus==='未审核'">编辑</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['medicine:record:remove']" v-if="scope.row.reviewStatus!=='审核完成'">删除</el-button>
          <el-button link type="primary" icon="Edit" @click="handleShenHe(scope.row)" v-hasPermi="['medicine:record:remove']" v-if="scope.row.reviewStatus==='未审核'">审核</el-button>
          <el-button link type="primary" icon="Edit" @click="handleShenHe(scope.row)" v-hasPermi="['medicine:record:remove']" v-if="scope.row.reviewStatus==='审核失败'">再次审核</el-button>
          <el-button link type="primary" icon="Edit" @click="handleShenHe(scope.row)" v-hasPermi="['medicine:record:remove']" v-if="scope.row.reviewStatus==='审核完成'">再次入库</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品出库记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="outrecordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出库类型" prop="storageOutType">
          <el-select v-model="form.storageOutType" placeholder="请选择出库类型">
            <el-option
              v-for="dict in medicine_our_record"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出库日期" prop="storageOutDate">
          <el-date-picker clearable
            v-model="form.storageOutDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择出库日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="员工id " prop="medicineAdminOut">
          <el-input v-model="form.medicineAdminOut" placeholder="请输入员工id " />
        </el-form-item>
        <el-form-item label="员工id    " prop="medicineAdminOrder">
          <el-input v-model="form.medicineAdminOrder" placeholder="请输入员工id    " />
        </el-form-item>
        <el-form-item label="制单日期" prop="orderDate">
          <el-date-picker clearable
            v-model="form.orderDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择制单日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态" prop="reviewStatus">
          <el-select v-model="form.reviewStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in medicine_shenhe"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工id   " prop="medicineAdminReview">
          <el-input v-model="form.medicineAdminReview" placeholder="请输入员工id   " />
        </el-form-item>
        <el-form-item label="审核日期" prop="reviewDate">
          <el-date-picker clearable
            v-model="form.reviewDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择审核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="采购价" prop="buyMoney">
          <el-input v-model="form.buyMoney" placeholder="请输入采购价" />
        </el-form-item>
        <el-form-item label="发售价" prop="sellMoney">
          <el-input v-model="form.sellMoney" placeholder="请输入发售价" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Outrecord">
import { listOutrecord, getOutrecord, delOutrecord, addOutrecord, updateOutrecord } from "@/api/medicine/outrecord";
import {useRouter,useRoute} from "vue-router";


const router=useRouter();
const route=useRoute();
const { proxy } = getCurrentInstance();
const { medicine_our_record, medicine_shenhe } = proxy.useDict('medicine_our_record', 'medicine_shenhe');

const outrecordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    storageOutId: null,
    storageOutType: null,
    reviewStatus: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);


/** 跳转编辑入库记录 **/
const handleShenHe=()=>{

}

/** 查询药品出库记录列表 */
function getList() {
  loading.value = true;
  listOutrecord(queryParams.value).then(response => {
    outrecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    storageOutId: null,
    storageOutType: null,
    storageOutDate: null,
    medicineAdminOut: null,
    medicineAdminOrder: null,
    orderDate: null,
    reviewStatus: null,
    medicineAdminReview: null,
    reviewDate: null,
    remarks: null,
    buyMoney: null,
    sellMoney: null,
    ext3: null,
    ext4: null,
    ext5: null
  };
  proxy.resetForm("outrecordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.storageOutId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加药品出库记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  router.push({
    path:'/medicine/outrecord/chakan',
    query:{
      storageOutId:row.storageOutId
    }
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["outrecordRef"].validate(valid => {
    if (valid) {
      if (form.value.storageOutId != null) {
        updateOutrecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOutrecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _storageOutIds = row.storageOutId || ids.value;
  proxy.$modal.confirm('是否确认删除药品出库记录编号为"' + _storageOutIds + '"的数据项？').then(function() {
    return delOutrecord(_storageOutIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('medicine/outrecord/export', {
    ...queryParams.value
  }, `outrecord_${new Date().getTime()}.xlsx`)
}

getList();
</script>
